package StudyCode.summary.myfile;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class FileCreateFileLock {
	public static void main(String[] argv) {
		try {
			File file = new File("E:\\迅雷下载\\test1.txt");
			FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

			FileLock lock = channel.lock();

			try {
				lock = channel.tryLock();
			} catch (OverlappingFileLockException e) {
				e.printStackTrace();
			}

			lock.release();

			channel.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
